package com.lltt.study.controller;

import com.alibaba.cloud.ai.advisor.DocumentRetrievalAdvisor;
import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.rag.DashScopeDocumentRetriever;
import com.alibaba.cloud.ai.dashscope.rag.DashScopeDocumentRetrieverOptions;
import com.lltt.study.config.DashScopeConfig;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.rag.retrieval.search.DocumentRetriever;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@Slf4j
public class BailianRagController {
    @Resource
    ChatClient chatClient;
    @Resource
    DashScopeApi dashScopeApi;
    /**
     * http://localhost:6018/bailian/rag/chat
     * http://localhost:6018/bailian/rag/chat?msg=A0001
     * @param msg
     * @return
     */
    @GetMapping("/bailian/rag/chat")
    public Flux<String>chat(@RequestParam(name = "msg",defaultValue = "暂时无法回答，请找人工客服") String msg){
        //1.RetrieverOptions参数配置
        DashScopeDocumentRetrieverOptions dashScopeDocumentRetrieverOptions = DashScopeDocumentRetrieverOptions.builder()
                .withIndexName("智能客服")// 知识库名称
                .build();
        //2 百炼平台RAG知识库构建器
        DocumentRetriever retriever = new DashScopeDocumentRetriever(dashScopeApi,dashScopeDocumentRetrieverOptions);

        return chatClient.prompt()
                .user(msg)
                .advisors(new DocumentRetrievalAdvisor(retriever))
                .stream()
                .content();
    }
}
